﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using System.Data.SqlClient;
using DAL;
using System.Text;
using System.Net;
namespace Election
{
    /// <summary>
    /// IsDelegate 的摘要说明
    /// </summary>
    public class IsDelegate : IHttpHandler
    {
        DataClassesDataContext lqDB = new DataClassesDataContext();
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string wtrxm = context.Request["wtrxm"].ToString();     //委托人姓名
            string wtrcard = context.Request["wtrcard"].ToString();  //委托人身份证号
            string wtrmobile = context.Request["wtmobile"].ToString(); //委托人联系方式
            string bwtrxm = context.Request["bwtrxm"].ToString();   //被委托人姓名
            string bwtrcard = context.Request["bwtrcard"].ToString(); //被委托人身份证号码 
            string xjbh = context.Request["xjbho"].ToString();        //当前选举编号
            int wtcs = 0;
            string bwtname = "";
            using (DataClassesDataContext lqDB = new DataClassesDataContext())
            {
                var result = from r in lqDB.Xm
                             where r.sfzh == bwtrcard
                             select r;
                foreach (var item in result)
                {
                    wtcs = item.sybwtcs;
                    bwtname = item.xmxm;
                }
                if (bwtrcard != "" && bwtrxm != "")
                {
                    if (bwtrxm != bwtname)
                    {
                        context.Response.Write("none");
                    }
                    else if (wtcs <= 0)
                    {
                        context.Response.Write("sorry");
                    }
                    else
                    {
                        #region
                        Random ran = new Random();
                        int value = ran.Next(1000, 10000);
                        DateTime now = DateTime.Now;
                        string yzm = value.ToString();
                        string jlxh = now.ToString("yyyyMMddHHmmssfff") + yzm;
                        //将委托记录插入到委托记录表中
                        Wtjl w = new Wtjl();
                        w.xjbh = xjbh;   //当前选举编号
                        w.jlbh = jlxh;
                        w.fssj = now;
                        w.wtfs = "W";    //网站委托
                        w.flag = '1';    //委托待定
                        w.ishf = '1';    //是否回访
                        w.issl = '1';    //是否受理
                        w.yzm = yzm;

                        //获取主机IP
                        string HostName = Dns.GetHostName();

                        IPHostEntry MyEntry = Dns.GetHostByName(HostName);
                        IPAddress MyAddress = new IPAddress(MyEntry.AddressList[0].Address);
                        w.ip = MyAddress.ToString();
                        // w.wtsdysj = ;  //委托书打印时间

                        w.wtname = wtrxm;
                        if (wtrcard != "")
                        {
                            w.wtrsfzh = wtrcard;
                        }
                        else
                        {
                            w.wtrsfzh = now.ToString("yyyyMMddHHmmssfff") + "X";
                        }
                        w.wtrmobile = wtrmobile;
                        w.bwtname = bwtrxm;
                        if (bwtrcard != "")
                        {
                            w.bwtrsfzh = bwtrcard;
                        }
                        else
                        {
                            w.bwtrsfzh = now.ToString("yyyyMMddHHmmssfff") + "Y";
                        }

                        lqDB.Wtjl.InsertOnSubmit(w);
                        lqDB.SubmitChanges();
                        context.Response.Write("success");


                        //当前被委托人的委托次数减一
                        var xmResult = lqDB.Xm.FirstOrDefault(r => r.sfzh == bwtrcard);
                        xmResult.sybwtcs = xmResult.sybwtcs - 1;
                        lqDB.SubmitChanges();
                        #endregion
                    }
                }
            }

        }
        //public void getMobile(HttpContext context)
        //{
        //    string xjbh = context.Request["xjbho"].ToString();        //当前选举编号
        //    //查询指定社区的联系方式
        //    string phone = "";
        //    using (DataClassesDataContext lqdb = new DataClassesDataContext())
        //    {
        //        var result = from r in lqdb.Xjdj
        //                     where r.xjbh == xjbh
        //                     select r;
        //        foreach (var item in result)
        //        {
        //            phone = item.xjphone;
        //        }
        //        context.Response.Write(phone);
        //    }

        //}
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}